package com.yyq.sqlSession;

import com.yyq.io.Resources;
import com.yyq.pojo.Configuration;
import com.yyq.pojo.MappedStatement;

import java.io.InputStream;
import java.lang.reflect.*;
import java.util.List;

/**
 * @Author yuanyueqiang
 * @Date 2021-11-27 22:45
 */
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration){
        this.configuration = configuration;
    }

    @Override
    public <T> List<T> selectList(String statementId, Object... params) throws Exception {
        //完成SimpleExecutor里的query方法调用
        Executor executor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<T> list = (List<T>) executor.query(configuration, mappedStatement, params);
        return list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> list = selectList(statementId, params);
        if(list.size()==1){
            return (T) list.get(0);
        }else {
            throw new RuntimeException("查询结果为空或者返回结果过多!");
        }
    }



    @Override
    public <T> T getMapper(Class<T> tClass) {
        //通过动态代理来获取实现方法
        Object o = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{tClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //1.获取StatementId
                String className = method.getDeclaringClass().getName();
                String methodName = method.getName();
                String statementId = className + "." + methodName;
                MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
                String queryType = mappedStatement.getQueryType();
                if("select".equals(queryType)){
                    //2.获取被调用方法的返回值类型
                    Type genericReturnType = method.getGenericReturnType();
                    //3. 判断返回是否为泛型
                    if(genericReturnType instanceof ParameterizedType){
                        List<Object> objects = selectList(statementId, args);
                        return objects;
                    }
                    return selectOne(statementId,args);
                }else{
                    return update(statementId,args);
                }

            }
        });
        return (T) o;
    }

    @Override
    public int insert(String statementId, Object... params) throws Exception {
        return update(statementId,params);
    }

    @Override
    public int update(String statementId, Object... params) throws Exception {
        Executor executor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        return executor.update(configuration, mappedStatement, params);
    }

    @Override
    public int delete(String statementId, Object... params) throws Exception {
        return update(statementId,params);
    }


}
